package com.speedata.xuyanshuai;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.speedata.uhftest.R;
import com.speedata.utils.HnweUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuyan  30口取货,右侧按钮与状态显示
 */
public class BoxTestActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;

    private Button button6;
    private Button button7;
    private Button button8;
    private Button button9;
    private Button button10;

    private Button button11;
    private Button button12;
    private Button button13;
    private Button button14;
    private Button button15;

    private Button button16;
    private Button button17;
    private Button button18;
    private Button button19;
    private Button button20;

    private Button button21;
    private Button button22;
    private Button button23;
    private Button button24;
    private Button button25;

    private Button button26;
    private Button button27;
    private Button button28;
    private Button button29;
    private Button button30;

    private Button button011;

    private Button button021;

    private Button button031;

    private Button button041;

    private TextView textView0;

    private List<Boolean> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_box_test);

        initView();

        mList = new ArrayList<>();
        for (int i = 0; i <= 40; i++) {
            mList.add(false);
        }
    }


    private void initView() {

        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        button5 = findViewById(R.id.button5);

        button6 = findViewById(R.id.button6);
        button7 = findViewById(R.id.button7);
        button8 = findViewById(R.id.button8);
        button9 = findViewById(R.id.button9);
        button10 = findViewById(R.id.button10);

        button11 = findViewById(R.id.button11);
        button12 = findViewById(R.id.button12);
        button13 = findViewById(R.id.button13);
        button14 = findViewById(R.id.button14);
        button15 = findViewById(R.id.button15);

        button16 = findViewById(R.id.button16);
        button17 = findViewById(R.id.button17);
        button18 = findViewById(R.id.button18);
        button19 = findViewById(R.id.button19);
        button20 = findViewById(R.id.button20);

        button21 = findViewById(R.id.button21);
        button22 = findViewById(R.id.button22);
        button23 = findViewById(R.id.button23);
        button24 = findViewById(R.id.button24);
        button25 = findViewById(R.id.button25);

        button26 = findViewById(R.id.button26);
        button27 = findViewById(R.id.button27);
        button28 = findViewById(R.id.button28);
        button29 = findViewById(R.id.button29);
        button30 = findViewById(R.id.button30);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);

        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button9.setOnClickListener(this);
        button10.setOnClickListener(this);

        button11.setOnClickListener(this);
        button12.setOnClickListener(this);
        button13.setOnClickListener(this);
        button14.setOnClickListener(this);
        button15.setOnClickListener(this);

        button16.setOnClickListener(this);
        button17.setOnClickListener(this);
        button18.setOnClickListener(this);
        button19.setOnClickListener(this);
        button20.setOnClickListener(this);

        button21.setOnClickListener(this);
        button22.setOnClickListener(this);
        button23.setOnClickListener(this);
        button24.setOnClickListener(this);
        button25.setOnClickListener(this);

        button26.setOnClickListener(this);
        button27.setOnClickListener(this);
        button28.setOnClickListener(this);
        button29.setOnClickListener(this);
        button30.setOnClickListener(this);


        button011 = findViewById(R.id.button_11);

        button021 = findViewById(R.id.button_21);

        button031 = findViewById(R.id.button_31);

        button041 = findViewById(R.id.button_41);


        button011.setOnClickListener(this);

        button021.setOnClickListener(this);

        button031.setOnClickListener(this);

        button041.setOnClickListener(this);


        textView0 = findViewById(R.id.textview_0);

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                if (changeList(1)) {
                    button1.setBackgroundColor(Color.BLUE);
                } else {
                    button1.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button2:

                if (changeList(2)) {
                    button2.setBackgroundColor(Color.BLUE);
                } else {
                    button2.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button3:
                if (changeList(3)) {
                    button3.setBackgroundColor(Color.BLUE);
                } else {
                    button3.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button4:
                if (changeList(4)) {
                    button4.setBackgroundColor(Color.BLUE);
                } else {
                    button4.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button5:
                if (changeList(5)) {
                    button5.setBackgroundColor(Color.BLUE);
                } else {
                    button5.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button6:
                if (changeList(6)) {
                    button6.setBackgroundColor(Color.BLUE);
                } else {
                    button6.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button7:
                if (changeList(7)) {
                    button7.setBackgroundColor(Color.BLUE);
                } else {
                    button7.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button8:
                if (changeList(8)) {
                    button8.setBackgroundColor(Color.BLUE);
                } else {
                    button8.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button9:
                if (changeList(9)) {
                    button9.setBackgroundColor(Color.BLUE);
                } else {
                    button9.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button10:
                if (changeList(10)) {
                    button10.setBackgroundColor(Color.BLUE);
                } else {
                    button10.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button11:
                if (changeList(11)) {
                    button11.setBackgroundColor(Color.BLUE);
                } else {
                    button11.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button12:
                if (changeList(12)) {
                    button12.setBackgroundColor(Color.BLUE);
                } else {
                    button12.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button13:
                if (changeList(13)) {
                    button13.setBackgroundColor(Color.BLUE);
                } else {
                    button13.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button14:
                if (changeList(14)) {
                    button14.setBackgroundColor(Color.BLUE);
                } else {
                    button14.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button15:
                if (changeList(15)) {
                    button15.setBackgroundColor(Color.BLUE);
                } else {
                    button15.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button16:
                if (changeList(16)) {
                    button16.setBackgroundColor(Color.BLUE);
                } else {
                    button16.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button17:
                if (changeList(17)) {
                    button17.setBackgroundColor(Color.BLUE);
                } else {
                    button17.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button18:
                if (changeList(18)) {
                    button18.setBackgroundColor(Color.BLUE);
                } else {
                    button18.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button19:
                if (changeList(19)) {
                    button19.setBackgroundColor(Color.BLUE);
                } else {
                    button19.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button20:
                if (changeList(20)) {
                    button20.setBackgroundColor(Color.BLUE);
                } else {
                    button20.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button21:
                if (changeList(21)) {
                    button21.setBackgroundColor(Color.BLUE);
                } else {
                    button21.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button22:
                if (changeList(22)) {
                    button22.setBackgroundColor(Color.BLUE);
                } else {
                    button22.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button23:
                if (changeList(23)) {
                    button23.setBackgroundColor(Color.BLUE);
                } else {
                    button23.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button24:
                if (changeList(24)) {
                    button24.setBackgroundColor(Color.BLUE);
                } else {
                    button24.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button25:
                if (changeList(25)) {
                    button25.setBackgroundColor(Color.BLUE);
                } else {
                    button25.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button26:
                if (changeList(26)) {
                    button26.setBackgroundColor(Color.BLUE);
                } else {
                    button26.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button27:
                if (changeList(27)) {
                    button27.setBackgroundColor(Color.BLUE);
                } else {
                    button27.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button28:
                if (changeList(28)) {
                    button28.setBackgroundColor(Color.BLUE);
                } else {
                    button28.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button29:
                if (changeList(29)) {
                    button29.setBackgroundColor(Color.BLUE);
                } else {
                    button29.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button30:
                if (changeList(30)) {
                    button30.setBackgroundColor(Color.BLUE);
                } else {
                    button30.setBackgroundColor(Color.WHITE);
                }
                break;
            case R.id.button_11:
                //开黄灯
                openYellow();
                break;

            case R.id.button_21:
                //开红灯
                openRed();
                break;

            case R.id.button_31:
                //开黄灯（按键）
                openYellow();
                break;

            case R.id.button_41:
                //开调试
                openDebug();
                break;

            default:
                break;
        }
    }

    /**
     * 亮红灯方法
     */
    private void openRed() {
        List<String> sList = realcmd("10");
        for (int i = 1; i < sList.size(); i++) {
            if (sList.get(i) != null && !"".equals(sList.get(i))) {
                Log.d("byte", sList.get(i));
                HnweUtils.goodsLight(sList.get(i));
            }
        }
    }

    /**
     * 调试方法
     */
    private void openDebug() {
        List<String> sList = realcmd("11");
        for (int i = 1; i < sList.size(); i++) {
            if (sList.get(i) != null && !"".equals(sList.get(i))) {
                Log.d("byte", sList.get(i));
                HnweUtils.goodsLight(sList.get(i));
            }
        }
    }

    /**
     * 亮黄灯方法（按键）
     */
    private void openYellow() {
        List<String> sList = realcmd("01");
        for (int i = 1; i < sList.size(); i++) {
            if (sList.get(i) != null && !"".equals(sList.get(i))) {
                Log.d("byte", sList.get(i));
                HnweUtils.goodsLight(sList.get(i));
            }
        }
    }

    private List<String> realcmd(String cmd) {
        List<String> stringList = new ArrayList<>();
        for (int i = 0; i <= 40; i++) {
            stringList.add("");
        }
        for (int i = 1; i < mList.size(); i++) {
            if (mList.get(i)) {
                stringList.set(i, getCmd(i, cmd));
            }
        }
        return stringList;
    }

    private String getCmd(int i, String cmd) {
        String a = String.valueOf(i / 4);
        String b = String.valueOf(i % 4);
        String c = cmd;
        Log.d("byte", a + b + c);
        return a + b + c;
    }

    private boolean changeList(int i) {
        mList.set(i, !mList.get(i));
        return mList.get(i);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode >= 132 && keyCode <= 160) {
            int box = keyCode - 130;
            String text = "箱子" + box + "已放入";
            Toast.makeText(BoxTestActivity.this, text, Toast.LENGTH_SHORT).show();
            textView0.setText(text);
        } else if (keyCode == 170) {
            String text = "箱子1已放入";
            Toast.makeText(BoxTestActivity.this, text, Toast.LENGTH_SHORT).show();
            textView0.setText(text);
        }
        return super.onKeyDown(keyCode, event);
    }
}